//
//  QSRegex.swift
//  TestSwift
//
//  Created by Mac on 2022/3/30.
//  Copyright © 2022 Song. All rights reserved.
//

import Foundation

struct QSRegex {
    let regex: NSRegularExpression?
    
    init(_ pattern: String) {
        regex = try? NSRegularExpression.init(pattern: pattern, options: .caseInsensitive)
    }
    
    /// 匹配
    /// - Parameter input: 内容
    /// - Returns: 是否匹配
    func match(_ input: String) -> Bool {
        if let matchs = regex?.matches(in: input, options: [], range: NSRange.init(location: 0, length: input.count)) {
            return matchs.count > 0
        }
        
        return false
    }
}

/// 运算符优先级
precedencegroup QSPrecedencegroup {
    associativity: none
    higherThan: MultiplicationPrecedence
}

/// 正则匹配
infix operator =~ : QSPrecedencegroup

func =~ (lhs: String, rhs: String) -> Bool {
    return QSRegex(rhs).match(lhs)
}
